class WindowManager(object):
    '''Class WindowManager:
    Simple User Interface Window Manager
    This class essentially wraps a single root window and handles input and drawing
    through a consistent interface.
    '''
    
    def __init__(self, screen=None, root=None):
        '''WindowManager.___init___:
        Initialize SUI manager with optional root window and screen.
        If screen is given but root is omitted, setup is automatically called with screen.
        Arguments: pygame.Surface screen=None, Window root
        '''
        self.root = root
        if screen is not None and root is None:
            self.setup(screen)
        else:
            self.screen = screen
            
    
    def __getitem__(self, key):
        '''WindowManager.__getitem__:
        Basic override of [] operator to allow root's children to easily be accessed.
        Arguments: String key 
        Returns: Value of root[key]
        '''
        return self.root[key]
    
    def setup(self, screen, size=None):
        '''WindowManager.setup:
        Initialize manager with screen and a root window of given size.
        If size is omitted, the root window will default to the same size as the screen.
        Arguments: pygame.Surface screen, tuple size (x, y) = None
        Returns: Nothing
        ''' 
        from window import Window; from pygame import Rect
        self.screen = screen
        self.root = Window("root", Rect(screen.get_rect()))

    def paint(self):
        '''WindowManager.paint:
        Paint entire window structure.
        Arguments: Nothing
        Returns: Nothing
        '''
        if self.root is None:
            raise Exception, "Window manager has no root window, cannot paint."
            
        self.root._paint(self.screen, 0, 0)
            
    def add(self, *args):
        '''WindowManager.add:
        Adds window or list of windows to root window.
        Arguments: one or more Window objects: add(w1, [w2, [w3...]]) 
        Returns: Nothing
        '''
        if self.root is None:
            raise Exception, "Window manager has no root window, cannot add."
        self.root.add(*args)
    
    def remove(self, name):
        '''WindowManager.remov:
        Removes specified window from root window.
        If window does not exist, Window.remove will fail instead of this. 
        Arguments: String name
        Returns: Nothing
        '''
        if self.root is None:
            raise Exception, "Window manager has no root window, you cannot remove from what does not exist."

        self.root.remove(name)
    
    def injectEvent(self, event):
        '''WindowManager.injectEvent:
        Inserts pygame events into the gui system.
        Any event type can be injected.
        Arguments: pygame.event.Event event
        Returns: True if the event should be ignored by any further program execution.
        False if the event was not exclusively handled by the GUI.
        '''
        return self.root._event(event)